package com.webserver.controllers;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.User;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class UserController {
    private static String usersDir="./users/";
    static {
        File userDir =new File(usersDir);
        if (!userDir.exists()){
            userDir.mkdirs();
        }
    }
    public void reg(HttpRequest request, HttpResponse response){
        System.out.println("开始处理用户注册！！！！");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String nickname=request.getParameter("nickname");
        String ageStr =request.getParameter("age");
        int age =Integer.parseInt(ageStr);
        System.out.println(username+","+password+","+nickname+","+age);
        try(FileOutputStream fos =new FileOutputStream(
                usersDir+username+".obj");
            ObjectOutputStream oos =new ObjectOutputStream(fos);
        ){
            User user =new User(username,password,nickname,age);
            oos.writeObject(user);
            File file =new File("./webapps/myweb/reg_success.html");
            response.setEntity(file);
        }catch (IOException e){
            e.printStackTrace();
        }

    }
}
